/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clusterchart;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import au.com.bytecode.opencsv.CSVReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import clusterchart.lib.DrawArrow;
import java.awt.*;

/**
 *
 * @author Libra
 */
public class Canvas extends javax.swing.JPanel {

    private Graphics2D g2d;
    private Integer[][] matrix;
    private int interval = 90;
    private int space = 2;
    private int barWidth = 6;
    private int x0 = 100;
    private int y0 = 10;
    private int width;
    private int height;
    private String file1 = "rsrc/GluM0";
    private String file2 = "rsrc/GluM1";
    private Color color1 = new Color(0, 65, 117, 255);
    private Color color2 = new Color(50, 130, 194, 160);
    private int[] marks = {80, 48, 80, 56, 35, 38, 13};
//    int scale;

    public void setBoth() {
        file1 = "rsrc/BothM0";
        file2 = "rsrc/BothM1";
        color1 = new Color(0, 65, 117, 255);
        color2 = new Color(50, 130, 194, 160);
        int[] m = {80, 48, 80, 56, 35, 38, 13};
        marks = m;
        repaint();
    }

    public void setGlu() {
        file1 = "rsrc/GluM0";
        file2 = "rsrc/GluM1";
        color1 = new Color(160, 0, 0, 255);
        color2 = new Color(255, 0, 0, 160);
        int[] m = {134,109,77,21,9,0,0};
        marks = m;
        repaint();
    }

    public void setGABA() {
        file1 = "rsrc/GABAM0";
        file2 = "rsrc/GABAM1";
        color1 = new Color(0, 160, 0, 255);
        color2 = new Color(0, 255, 0, 160);
        int[] m = {127,82,99,27,14,1,0};
        marks = m;
        repaint();
    }

    @Override
    public boolean isOpaque() {
        return true;
    }

    @Override
    protected void paintComponent(Graphics g) {
        /*
         * Draw the stats.
         */

        g2d = (Graphics2D) g;

        setDefaultPara();

        readMatrix(file1);
        drawMatrix(color1);
        readMatrix(file2);
        drawMatrix(color2);
        drawMarks();

    }

    private void drawMatrix(Color c) {
        int x;
        int y;
        int z;

        for (int i = 0; i < matrix.length; i++) {
            y = i;
            for (int j = 0; j < matrix[i].length; j++) {
                x = j;
                z = matrix[i][j];
//                System.out.println(x + "," + y + "," + z);
                if (z > 1) {
                    drawBar(x, y, z, c);
                }
            }
        }
    }

    private void setDefaultPara() {
        width = super.getWidth();
        height = super.getHeight();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
//        dataFont = new Font("Serif", Font.PLAIN, 20);
//        idFont = new Font("Serif", Font.PLAIN, 28);

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);


    }

    private void drawBar(int xx, int yy, int zz, Color c) {

        g2d.setColor(c);

        if (zz > 50 / barWidth) {

            int yfull = height;
            int xCenter = xx * interval + x0;
            int y = yfull - yy * space - y0;
            int z = zz / (100 / barWidth);


            Point2D.Double pC = new Point2D.Double(xCenter, y);
            Point2D.Double pL = new Point2D.Double(xCenter - z, y);
            Point2D.Double pR = new Point2D.Double(xCenter + z, y);

            g2d.draw(new Line2D.Double(pL, pC));
        }

    }

    private void readMatrix(String pathToFile) {
        String matrixFile = pathToFile;

        try (CSVReader reader = new CSVReader(new InputStreamReader(
                        Canvas.class.getResourceAsStream(matrixFile)))) {
            List<String[]> matrixList = reader.readAll();
//            System.err.println(matrixList.size()+","+matrixList.get(0).length);
            matrix = new Integer[matrixList.size()][matrixList.get(0).length];
            for (int i = 0; i < matrixList.size(); i++) {
                for (int j = 0; j < matrixList.get(i).length; j++) {
                    String s = matrixList.get(i)[j];
                    matrix[i][j] = s.equals("") ? 0 : Integer.parseInt(s);
                }
            }

        } catch (IOException e) {
            System.err.println(e.toString());
        }
    }

    private void drawMarks() {
        int markLength = 20;
        int arrowLength = 10;

        int yfull = super.getHeight();
        DrawArrow arrow = new DrawArrow();
        arrow.setGraphics(g2d);
        arrow.setArrowAngle(30);
        arrow.setArrowLength(arrowLength);
        for (int i = 0; i < marks.length; i++) {

            int xCenter = i * interval + x0;
            int y = yfull - marks[i] * space - y0;

            Point2D.Double fP = new Point2D.Double(xCenter - markLength, y);
            Point2D.Double tP = new Point2D.Double(xCenter, y);

            g2d.setColor(Color.BLACK);

            arrow.drawArrow(fP, tP);

        }

    }

    /**
     * Creates new form Canvas
     */
    public Canvas() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
